package com.itheima.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Dept;
import com.itheima.service.system.DeptService;
import com.itheima.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/dept")
public class DeptController extends BaseController {

    @Autowired
    protected DeptService deptService;

    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(defaultValue = "1") String pageNum,
                             @RequestParam(defaultValue = "5") String pageSize) {
        String companyId = getLoginCompanyId();
        PageInfo<Dept> byPage = deptService.findByPage(companyId, Integer.valueOf(pageNum), Integer.valueOf(pageSize));
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/system/dept/dept-list");
        mv.addObject("pageInfo", byPage);
        return mv;
    }

    @RequestMapping("/toAdd")
    public String toAdd(HttpServletRequest request) {
        String companyId = getLoginCompanyId();
        List<Dept> all = deptService.findAll(companyId);
        request.setAttribute("deptList", all);
        return "system/dept/dept-add";
    }

    @RequestMapping("/edit")
    public String edit(Dept dept) {
        // 设置企业信息
        dept.setCompanyId(getLoginCompanyId());
        dept.setCompanyName(getLoginCompanyName());

        // 判断
        if (StringUtils.isEmpty(dept.getId())){
            deptService.save(dept);
        }else {
            deptService.update(dept);
        }

        // 重定向到列表
        return "redirect:/system/dept/list.do";
    }

    /**
     * 4. 进入修改页面
     * http://localhost:8080/system/dept/toUpdate.do?id=100
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id,Model model){
        String companyId = getLoginCompanyId();
        //1. 根据部门id查询
        Dept dept = deptService.findById(id);
        //2. 查询所有部门
        List<Dept> deptList = deptService.findAll(companyId);
        //3. 保存
        model.addAttribute("dept",dept);
        model.addAttribute("deptList",deptList);
        //4. 转发
        return "system/dept/dept-update";
    }

    /**
     * 5. 删除
     * result = {"message":"删除成功！"}
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, String> delete(String id) {
        HashMap<String, String> result = new HashMap<>();
        boolean flag = deptService.delete(id);
        //3. 判断
        if (flag) {
            // 删除成功
            result.put("message","删除成功!");
        } else {
            result.put("message","删除失败：删除的部门有子部门，不能删除！");
        }
        return result;
    }

}
